package com.wuwei.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wuwei.handler.CustomerExceptionHandler;
import com.wuwei.pojo.CommonResult;
import com.wuwei.pojo.Payment;
import com.wuwei.service.TestService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @description: TestController <br>
 * @date: 2021/2/21 16:08 <br>
 * @author: wuwei666 <br>
 * @version: 1.0 <br>
 */
@Slf4j
@RestController
public class TestController {
    @Autowired
    private TestService testService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/alibaba/sentinel")
    public String hello() {
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel2")
    public String hello2() {
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel3")
    public String hello3() {
        try {
            Thread.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel4")
    public String hello4() {
        log.info(Thread.currentThread().getName() + new Date().toString());
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel5")
    public String hello5() {
        log.info(Thread.currentThread().getName() + new Date().toString());
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel6/{millis}")
    public String hello6(@PathVariable("millis") Integer millis) {

        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + new Date().toString());
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/alibaba/sentinel7/{num}")
    public String hello7(@PathVariable("num") Integer num) {

        if (num == 4){
            throw new IllegalArgumentException("参数异常");
        }

        log.info(Thread.currentThread().getName() + new Date().toString());
        return "hello alibaba sentinel" + " 端口： " + port + testService.linkTest();
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_hotKey")
    public String testHotKey(@RequestParam(value = "p1", required = false) Integer p1,
                             @RequestParam(value = "p2", required = false) Integer p2){
        return ">>>>>>>>>>>>>>>>>>>>testHotKey>>>>>>>>(*^_^*)";
    }
    public String deal_hotKey(@RequestParam(value = "p1", required = false) Integer p1,
                              @RequestParam(value = "p2", required = false) Integer p2,
                              BlockException exception){
        return ">>>>>>>>>>>>>>>>>>>>>>>>>>deal_hotKey>>>>/(ㄒoㄒ)/~~";
    }

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handlerByResource")
    public CommonResult<Payment> byResource(){
        return new CommonResult<Payment>(200, "按照资源名称限流测试ok", new Payment(1L, UUID.randomUUID().toString()));
    }
    public CommonResult<Payment> handlerByResource(BlockException exception){
        return new CommonResult<Payment>(444, "error,服务忙>>>", null);
    }

    @GetMapping("/byResource2")
    @SentinelResource(value = "byResource2",blockHandlerClass = CustomerExceptionHandler.class, blockHandler = "handlerException")
    public CommonResult<Payment> byResource2(){
        return new CommonResult<Payment>(200, "按照资源名称限流测试ok", new Payment(1L, UUID.randomUUID().toString()));
    }


}
